package com.bestdreamer.example.publish;

import com.bestdreamer.annoations.Recommend;
import com.bestdreamer.annoations.ThreadSafe;

/**
 * 使用枚举来实现单例模式：最安全的
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {

    private SingletonExample7(){

    }

    public static SingletonExample7 getInstance(){
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton{
        INSTANCE;

        private final SingletonExample7 singleton;

        // JVM 保证这个方法绝对只调用一次
        Singleton(){
            singleton = new SingletonExample7();
        }
        public SingletonExample7 getInstance(){
            return singleton;
        }
    }

}
